/**
 * Created by DELL on 2017/5/9.
 */
common.buildNormalEnumSelect(".lockFixStatus", enumObject.enumJsonData['lockFixStatus']);//创建 装锁状态 枚举列表
// common.buildNormalEnumSelect(".rewardStatus", enumObject.enumJsonData['rewardStatus']);//创建跟进状态枚举列表
/**
 * 中介联系人 列表初始化设置
 * @type {Object}
 */
var settings = {
    "sAjaxSource": window.globalHost + common.esheyiHouseListPath,
    "aoColumns": [{
        "mDataProp": function (obj) {
            return "<a href='../submenu.html?key=esheyiHouse&id=" + obj.id + "&houseNo="+obj.contractNo+"'>" + (obj.contractNo ? obj.contractNo : "无编号") + "</a>";
        },
        "sTitle": "签约合同编号",
        "sDefaultContent": "",
        "sClass": "center"
    }, {
        "mDataProp": function (obj) {
            return (!!obj.villageName ? obj.villageName : "无信息") + "<br/>" + (obj.houseAddress ? obj.houseAddress : "无信息");
        },
        "sTitle": "小区/房源地址",
        "sDefaultContent": ""
    }, {
        "mDataProp": function (obj) {
            return !!obj.houseFloorArea ? obj.houseFloorArea + "" : "无信息"
        },
        "sTitle": "面积(平米)",
        "sDefaultContent": ""
    }, {
        "mDataProp": "createTime",
        "sTitle": "创建时间",
        "sDefaultContent": ""
    }, {
        "mDataProp": function (obj) {
            return '<a href="javascript:;" class="QrCode" data-id="' + obj.id + '" data-houseAddress="'+(obj.houseAddress ? obj.houseAddress : "无信息")+'">点击查看</a>';
        },
        "sTitle": "查看二维码",
        "sDefaultContent": ""
    },{
        "mDataProp": function (obj) {
            // var onlineStatus = obj.lockType == 1 ? (!!obj.wifiOnlineStatus ? "<br/>" + obj.wifiOnlineStatus : '<br/>无线上状态') : '';
            return enumObject.enumJsonData['lockType'][obj.lockType];
        },
        "sTitle": "锁类型",
        "sDefaultContent": ""
    }, {
        "mDataProp": function (obj) {
            if (obj.lockType == 1) {
                return enumObject.enumJsonData['lockFixStatus'][obj.lockFixStatus];
            } else {
                return "非智能锁"
            }
        },
        "sTitle": "装锁状态",
        "sDefaultContent": ""
    }, {
        "mDataProp": function (obj) {
            var remark=obj.remark.replace(common.regWarp,'<br/>');
            if(remark===""){
                return "无备注";
            }else{
                return '<a class="remark" href="javascript:;" data-remark="'+remark+'">'+remark.substr(0,20)+"..."+'</a>';
            }
        },
        "sTitle": "备注",
        "sDefaultContent": "",
    }, {
        "mDataProp": function (obj) {
            var bindBtn = "";
            if (obj.lockType == 1) {
                if (obj.lockFixStatus == 0) {
                    bindBtn = '<a href="javascript:;" class="btn btn-primary lock-install" data-id="' + obj.id + '">申请装锁</a>';
                } else  {
                    bindBtn = '<a href="javascript:;" class="btn btn-warning lock-sync" data-id="' + obj.id + '">同步</a>';

                }
            }
            if (obj.isHouseOnReward == 'no') {
                return "<a href='javascript:;' class='btn btn-success start-reward' data-id='" + obj.id + "'>发起悬赏</a>" + bindBtn;
            } else {
                return "<a href='javascript:;' class='btn btn-default'>已悬赏</a>" + bindBtn;
            }
        },
        "sTitle": "操作",
        "sDefaultContent": "",
        sClass: "clearfix text-center"
    }],
    "fnServerParams": function (aoData) {
        return common.getSearchData("form.data-list-search", aoData);
    }
};
var options = $.extend(true, {}, _dataTableSettings, settings);
/**
 * 实例化 中介联系人 列表插件
 * @type {[type]}
 */
var $currentDataTable = $("#data-list");
var oTable = $currentDataTable.dataTable(options);
searchList.getDataList(oTable);
searchList.resetSearch(oTable);
var userInfo = common.getLocalData(common.agentSysCacheWord).user;

$("button.insertHouse").on('click', function (ev) {
    common.openInsertDialog({
        'url': './rewardHouse/insertHouse.html',
        'title': '添加房源',
        'sAjaxSource': window.globalHost + common.esheyiHouseSavePath,
        'area': ['1000px', '600px'],
        ok: function (data) {
            //  新增信息成功之后，刷新列表页面
            oTable.fnClearTable();
        },
        btn: ['保存', '取消'],
        cancel: function () {
        }
    });
});
$currentDataTable.off("click", ".remark").on("click", ".remark", function (e) {
    var remark = $(this).attr("data-remark");
    common.openTempDialog({
        title: '房源备注',
        area:['460px', '300px'],
        content: '<div style="padding:10px;width:100%;word-break:break-all;">'+remark+'</div>',
        btn:[]
    });
});
$currentDataTable.off("click", ".start-reward").on("click", ".start-reward", function (e) {
    var houseId = $(this).attr("data-id");
    common.confirm({msg: "是否确定发起悬赏?"}).then(function () {
        common.openInsertDialog({
            'url': './rewardHouse/insertReward.html?houseId=' + houseId,
            'title': '悬赏信息',
            'sAjaxSource': window.globalHost + common.StartRewardPath,
            'area': ['1000px', '600px'],
            ok: function () {
                //  新增信息成功之后，刷新列表页面
                oTable.fnClearTable();
            },
            btn: ['保存', '取消'],
            cancel: function () {
            }

        })
    });
});
$currentDataTable.off("click", ".lock-install").on("click", ".lock-install", function (e) {
    var $this = $(this);
    var houseId = $this.attr("data-id");
    var sendData = {id: houseId, userId: userInfo.id};
    common.updateLock({
        confirmMsg: "确定申请装锁?",
        path: common.addLockPath,
        sendData: sendData,
        listPanel:oTable
    });
});
$currentDataTable.off("click", ".lock-sync").on("click", ".lock-sync", function (e) {
    var $this = $(this);
    var houseId = $this.attr("data-id");
    var sendData = {id: houseId, userId: userInfo.id};
    common.updateLock({
        confirmMsg: "是否同步锁信息?",
        path: common.syncHouseLockPath,
        sendData: sendData,
        listPanel:oTable
    });
});
var $QrCode=$("#QR-code");
$currentDataTable.off("click", ".QrCode").on("click", ".QrCode", function (e) {
    var $this = $(this);
    var houseId = $this.attr("data-id");
    var houseAddress = $this.attr("data-houseAddress");
    var links=window.visitedSubmitLink;
    links+="?houseId="+houseId;
    links=common.toUtf8(links);
    $QrCode.html("").qrcode({
        id:'myCanvas',
        render:'canvas',
        width:600,
        height:600,
        text:links
    });
    var canvas=$QrCode.find("canvas")[0];
    var base64=canvas.toDataURL("image/jpeg",0.98);
    var content='<p style="text-align:center;height:60px;line-height: 60px;margin:0;font-size: 14px;font-weight:bold;">'+(houseAddress ? houseAddress:"无信息")+'</p>' +
        '<p style="width:100%;margin:0;text-align:center;"><img style="width:300px;" src="'+base64+'" alt=""></p>';
    layer.open({
        type:1,
        title:false,
        shadeClose:true,
        content:content,
        area:['600px','400px']
    });
});